package com.zh.risk.quartz.merchant;

import com.ql.util.express.ExpressRunner;
import com.zh.risk.quartz.ab.QlRunnerAdapter;
import org.jetbrains.annotations.Contract;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 用于实现引擎执行的公共方法
 * @Author: Liard
 * @Create in: 2020/7/15 9:54
 * @Version: 1.0.1
 */
public class QlRunner extends QlRunnerAdapter {

    private static final Logger log = LoggerFactory.getLogger(QlRunner.class);
    private static QlRunner qlRunner = new QlRunner();
    @Contract(pure = true)
    public static QlRunner getInstance() { return qlRunner; }

    @Override
    public ExpressRunner getRunner() throws Exception {
        ExpressRunner runner = new ExpressRunner();
        //  定义中文字符表示
        runner.addOperatorWithAlias("如果", "if", null);
        runner.addOperatorWithAlias("否则", "else", null);
        runner.addOperatorWithAlias("大于", ">", null);
        runner.addOperatorWithAlias("小于", "<", null);
        runner.addOperatorWithAlias("大于等于", ">=", null);
        runner.addOperatorWithAlias("小于等于", "<=", null);
        runner.addOperatorWithAlias("并且", "&&", null);
        runner.addOperatorWithAlias("或者", "||", null);
        runner.addOperatorWithAlias("不等于", "!=", null);
        runner.addOperatorWithAlias("等于", "==", null);
        runner.addOperatorWithAlias("返回", "return", null);
        return runner;
    }

    @Override
    public ExpressRunner getRunner(@NonNull Map<String, String> map) throws Exception {
        ExpressRunner runner = new ExpressRunner();
        getRunner();
        for (Map.Entry<String,String> m: map.entrySet()) {
            runner.addOperatorWithAlias(m.getKey(), m.getValue(), null);
        }
        return runner;
    }
}
